#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :weibo_class版.py
# @Time      :2024/11/11 
# @Author    :CL
# @email     :1037654919@qq.com
# class 版本实现cookies的自动更新
import os
import time
from retrying import retry
from datetime import datetime, timedelta
import requests
from utils import mongo_manager
weibo_user = mongo_manager(collect_name='weibo_user',db='cl_data')
weibo_blog = mongo_manager(collect_name='weibo_blog',db='cl_data')
weibo_comments = mongo_manager(collect_name='weibo_comments',db='cl_data')

from cookies import cookies
# 创建一个CookieJar对象
cookie_jar = requests.cookies.RequestsCookieJar()

for cookie in cookies:
    cookie_jar.set(
        name=cookie['name'],
        value=cookie['value'],
        domain=cookie['domain']
    )
class Weibo:
    def __init__(self):
        self.cookies =cookie_jar
        self.headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "client-version": "v2.46.18",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://weibo.com/u/2047010597",
        "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "server-version": "v2024.09.26.1",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
        "x-xsrf-token": "Mc2Cr-5qSBSOo4eD1HX4Nvu2"
    }


    # 获取用户信息
    def get_uinfo(self):

        url = "https://weibo.com/ajax/profile/info"
        params = {
            "uid": "2047010597"
        }
        response = requests.get(url, headers=self.headers, cookies=self.cookies, params=params)
        print(response.url, response)
        # print(response.text)
        return response.json()['data']['user']
    # 获取微博
    def get_mymblog(self,page=1):
        url = "https://weibo.com/ajax/statuses/mymblog"
        params = {
            "uid": "3919603060", 
            "page": page,
            "feature": "0"
        }
        response = requests.get(url, headers=self.headers, cookies=self.cookies, params=params)
        print(response.url, response)

        # print(response.text)
        return response.json()['data']['list']
    # 获取评论
    @retry(stop_max_attempt_number=3)
    def get_comment(self,params=None):
        url = "https://weibo.com/ajax/statuses/buildComments"
        if params == None:
            params = {
                "flow": "1",  # 按时间
                "is_reload": "1",
                "id": "5067347088642071",
                "is_show_bulletin": "2",
                "is_mix": "0",
                "count": "20",
                # "type": "feed",
                "max_id": 0,

                "uid": "2047010597",
                "fetch_level": "0",
                "locale": "zh"
            }

        response = requests.get(url, headers=self.headers, cookies=self.cookies, params=params, timeout=10)
        print(response.url, response)
        # print(response.text)
        return response.json()
    #如果cookies过期，重新获取
    def get_cookies(self):
        from DrissionPage import ChromiumPage
        page = ChromiumPage()
        # 第一个标签页访问网址
        page.get('https://weibo.com/u/3919603060')
        # print(page.cookies())
        # 下滑页面，随机等待
        page.scroll.to_bottom()
        time.sleep(5)
        cookies = page.cookies()  # list
        # 保存cookies为 pythoon cookies格式
        with open('cookies.py', 'w') as f:
            f.write('cookies = ' + str(cookies))
        page.close()
        # 重新读取cookies.py 的cookies 赋给self
        if os.path.exists('cookies.py'):
            from cookies import cookies
            # 创建一个CookieJar对象
            cookie_jar = requests.cookies.RequestsCookieJar()

            for cookie in cookies:
                cookie_jar.set(
                    name=cookie['name'],
                    value=cookie['value'],
                    domain=cookie['domain']
                )
            self.cookies = cookie_jar
            # print(self.cookies)
            return cookie_jar
        else:
            print("cookies.py文件不存在，无法重新读取cookies。")
# 实例化
wb = Weibo()
def save_uinfo():
    uinfo = wb.get_uinfo()
    user = {}
    user['id'] = uinfo['id']
    user['screen_name'] = uinfo['screen_name']
    user['转评赞'] = uinfo['status_total_counter']['total_cnt']
    user['累计评论'] = uinfo['status_total_counter']['comment_cnt']
    user['累计转发'] = uinfo['status_total_counter']['repost_cnt']
    user['累计获赞'] = uinfo['status_total_counter']['like_cnt']
    user['认证'] = uinfo['verified_reason']
    user['描述'] = uinfo['description']
    user['粉丝量'] = uinfo['followers_count']
    user['关注量'] = uinfo['friends_count']
    timestamp_seconds = int(datetime.now().timestamp())
    user["_id"] = f"{uinfo['id']}_{timestamp_seconds}"
    user['时间戳'] = timestamp_seconds
    # 格式化当前时间为标准形式
    now = datetime.now()
    current_time = now.strftime("%Y%m%d %H:%M:%S")

    user['时间'] =current_time
    try:
        weibo_user.insertOne(user)
    except Exception as e:
        print(e)
def save_myblog():
    for page in range(1,3):
        mymblog = wb.get_mymblog(page=page)
        for blog in mymblog:
            reslut = {}
            reslut['_id'] = blog['id']  # created_at
            reslut['uid'] = blog['user']['id']
            # 原始时间字符串
            time_str = blog['created_at']
            # 解析时间字符串
            dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
            # 格式化为所需格式
            formatted_time = dt.strftime("%Y%m%d %H:%M")
            reslut['time'] = formatted_time
            reslut['text_raw'] = blog['text_raw'] if blog['text_raw'] != '转发微博' else '转发微博：' + blog['retweeted_status'][
                'text_raw']
            try:
                weibo_blog.insertOne(reslut)
            except Exception as e:
                print(e)

def save_comments(reslut):
    max_id =0
    params = {
        "flow": "1",  # 按时间
        "is_reload": "1",
        "id": reslut['_id'],
        "is_show_bulletin": "2",
        "is_mix": "0",
        "count": "20",
        'max_id': max_id,
        "uid": reslut['uid'],
        "fetch_level": "0",
        "locale": "zh"
    }
    while True:
        comments = wb.get_comment(params=params)
        params['max_id'] = comments['max_id'] if 'max_id' in comments else 0
        print (reslut['_id'],params['max_id'])
        if params['max_id'] == 0:
            break
        for comment in comments['data']:
            for comment2 in comment['comments']: # 保存二级评论
                reslut2 = {}
                reslut2["_id"] = comment2['id']
                # if weibo_comments.findOne({'_id': comment2['id']}):
                #     continue
                reslut2["评论type"] ='二级评论'
                reslut2['微博id'] = reslut['_id']
                reslut2["微博原文"] = reslut['text_raw']
                reslut2["一级评论文本"] =  comment['text_raw']
                reslut2["来源"] = comment2['source']
                reslut2["评论用户"] = comment2['user']['screen_name']
                time_str = comment2['created_at']
                # 解析时间字符串
                dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
                # 格式化为所需格式
                formatted_time = dt.strftime("%Y%m%d %H:%M")
                reslut2['time'] = formatted_time
                reslut2["评论序号"] = comment2['floor_number']
                reslut2["二级评论文本"] = comment2['text_raw']
                reslut2["点赞量"] = comment2['like_counts']
                try:
                    weibo_comments.insertOne(reslut2)
                except Exception as e:
                    weibo_comments.updateOne({"_id":reslut2['_id']},reslut2)
            reslut1 = {}
            reslut1["_id"] = comment['id']  #
            # if weibo_comments.findOne({"_id": comment['id']}):
            #     continue
            reslut1["评论type"] = '一级评论'
            reslut1['微博id'] = reslut['_id']
            reslut1["微博原文"] = reslut['text_raw']
            reslut1["一级评论文本"] = comment['text_raw']
            reslut1["来源"] = comment['source']
            reslut1["评论用户"] = comment['user']['screen_name']
            time_str = comment['created_at']
            # 解析时间字符串
            dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
            # 格式化为所需格式
            formatted_time = dt.strftime("%Y%m%d %H:%M")
            reslut1['time'] = formatted_time
            reslut1["评论序号"] = comment['floor_number']
            reslut1["点赞量"] = comment['like_counts']
            reslut1["回复量"] = comment['total_number']
            try:
                weibo_comments.insertOne(reslut1)
            except Exception as e:
                weibo_comments.updateOne({"_id":reslut1["_id"]},reslut1)

def save_main():

    # #先试用cookies 是否失效，失效的话 运行；一般一个月失效。
    # wb.get_cookies()

    save_uinfo()
    save_myblog()
    # 获取当前日期
    current_date = datetime.now()
    # 计算60天前的日期
    sixty_days_ago = current_date - timedelta(days=60)
    # 将日期格式化为字符串yyyymmdd
    sixty_days_ago_str = sixty_days_ago.strftime("%Y%m%d")
    print(sixty_days_ago_str)
    # 执行查询，查找 'time' 字段大于 'sixty_days_ago' 的文档
    seeds = weibo_blog.findAll({"time": {"$gt": sixty_days_ago_str}})
    for seed in seeds:
        save_comments(seed)
        weibo_blog.updateOne({'_id':seed['_id']},{'status_comment':'done'})
        # break

if __name__ == '__main__':
    save_main()
